package ClientCommTests;

import static org.junit.Assert.*;

import org.junit.Test;

import client.TelnetClient;
import clientCommunicationHandler.ReadNewMsg;
import clientCommunicationHandler.UserInterfaceMsgHandler;

public class UserInterfaceMsgHandlerTest {

	@Test
	public void  handleNewPostMsgTest() {
		String postMsg="";
		UserInterfaceMsgHandler handler = new UserInterfaceMsgHandler();
		ReadNewMsg tMsg = new ReadNewMsg();
		String msg = tMsg.readMsgFromUser();
		
		String temp = "addPost\n"+TelnetClient.userName+"\n"+msg+"\n"+Integer.toString(TelnetClient.SubForumId)+"\n"
		+Integer.toString(TelnetClient.threadId)+"\n"+"11"+"\n\0\n"+
		handler.refreshLastViewedThread();
		
		
		postMsg=handler.handleNewPostMsg("11");
		
		assertEquals(temp, postMsg);
	}
	
	
	@Test
	public void BuildViewSubForumMsgTest() {
		String subForumId;
		String temp;
		UserInterfaceMsgHandler handler = new UserInterfaceMsgHandler();
		TelnetClient.SubForumId = Integer.parseInt("111");
		temp =  "viewSubForum\n" + "111"+"\n\0\n";
		subForumId = handler.BuildViewSubForumMsg("111");
		assertEquals(temp, subForumId);
	}
	
	public String refreshLastViewedThread(){
		return "viewPost\n"+TelnetClient.SubForumId+"\n"+TelnetClient.threadId +"\n\0\n";
	}
	
	@Test
	public void refreshLastViewedThreadTest(){
		UserInterfaceMsgHandler handler = new UserInterfaceMsgHandler();
		String tmp = handler.refreshLastViewedThread();
		String test =  "viewPost\n"+TelnetClient.SubForumId+"\n"+TelnetClient.threadId +"\n\0\n";
		assertEquals(tmp, test);
	}
	
	
	@Test
	public void refreshViewTest(){
		UserInterfaceMsgHandler handler = new UserInterfaceMsgHandler();
		String tmp = handler.refreshView();
		String test = "";
		if(TelnetClient.threadId > 0){
			test = refreshLastViewedThread();
		}
		else{
			if(TelnetClient.SubForumId > 0){
				test = handler.refreshLastViewedsubForum();
			}
		}
		if(test.equals(""))
			TelnetClient.transmitToServer = false;
	
		assertEquals(tmp, test);
	}
	
	
	@Test
	public void handleBackMsgTest(){
		UserInterfaceMsgHandler handler = new UserInterfaceMsgHandler();
		String temp = "";
		if(TelnetClient.postId > 0){
			temp  = handler.BuildViewThreadMsg(Integer.toString(TelnetClient.threadId));
		}
		else{
			if(TelnetClient.SubForumId > 0){
				handler.BuildViewSubForumMsg(Integer.toString(TelnetClient.SubForumId));
			}
			else{
				TelnetClient.menu = TelnetClient.welcom_page;
				TelnetClient.transmitToServer = false;
			}
		}
		
		String test = handler.handleBackMsg();
		assertEquals(temp, test);
		
	}

	
	
	

	
	
	
	

}
